package org.jeecg.modules.ncmeapis.data.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import ncme.digital.hszy.common.utils.StatisticUtil;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.swagger.BusinessApi;
import org.jeecg.modules.ncmeapis.data.entity.DataTemp;
import org.jeecg.modules.ncmeapis.data.entity.DataTempAdd;
import org.jeecg.modules.ncmeapis.data.service.IDataTempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@BusinessApi
@Api(tags = "临时数据")
@RestController
@RequestMapping("/auto/data/temp")
@Slf4j
public class DataTempController {

    @Autowired
    private IDataTempService dataTempService;

    @ApiOperation(value = "添加临时数据", notes = "category:数据集类别【demo】...<br/>" +
            "name:数据键【demo】...<br/>" +
            "type:数据类型(非必填,默认String)【JSONObject】...<br/>" +
            "category和name相同时，会覆盖原来的值")
    @PostMapping(value = "/set")
    public Result<String> getTempData(@RequestBody @Valid DataTempAdd dataTempAdd) throws Exception {
        dataTempService.setTempData(dataTempAdd.getCategory(), dataTempAdd.getName(), dataTempAdd.getValue());
        return Result.OK("成功");
    }

    @ApiOperation(value = "按类别和键获取", notes = "" +
            "category:数据集类别【demo】...<br/>" +
            "name:数据键【demo】...<br/>" +
            "type:数据类型(非必填)【JSONObject】...<br/>" +
            "wrap:是否包装(非必填)Result，默认true...<br/>"
    )
    @GetMapping(value = "/{category}")
    public Object getTempData(@PathVariable String category,
                              @RequestParam(name = "name") String name,
                              @RequestParam(name = "type", required = false) String type,
                              @RequestParam(name = "wrap", defaultValue = "true") boolean wrap) throws Exception {
        Object result = dataTempService.getTempData(category, name, type);
        if (wrap) return Result.OK("成功", result);
        else return result;
    }

    @ApiOperation(value = "获取所有临时数据", notes = "不传category就是全部")
    @GetMapping(value = "/all")
    public Object all(@RequestParam(name = "category", required = false) String category) {
        List<DataTemp> list = dataTempService.list();
        Map<String, List<JSONObject>> stringListMap = StatisticUtil.classifyList(list, DataTemp::getCategory, new Function<DataTemp, JSONObject>() {
            @Override
            public JSONObject apply(DataTemp dataTemp) {
                return new JSONObject() {{
                    this.put("name", dataTemp.getName());
                    this.put("value", dataTemp.getValue());
                    this.put("type", dataTemp.getType());
                    this.put("createTime", dataTemp.getCreateTime());
                }};
            }
        });
        if (StringUtils.isNotEmpty(category)) return Result.OK("成功", stringListMap.get(category));
        else return Result.OK("成功", stringListMap);
    }

}
